<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width:100%"
  >
    <el-col :span="20">
      <i v-if="isRoot" class="el-icon-phone" />
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row>
        <el-col :span="12">{{ nodeData.manager }}</el-col>
        <el-col :span="12">
          <el-dropdown @command="handleCommand">
            <span @click.stop> 操作<i class="el-icon-arrow-down" /> </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                <el-dropdown-item v-if="!isRoot" command="edit">查看部门</el-dropdown-item>
                <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartment } from '@/api/departments'
export default {
  name: 'TreeTools',
  props: {
    nodeData: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handleCommand(command) {
      // console.log('执行了')
      // console.log(command)
      switch (command) {
        case 'add':
          this.add()
          break
        case 'edit':
          this.edit()
          break
        case 'del':
          this.del()
          break
      }
    },
    add() {
      this.$emit('addDepart', this.nodeData)
    },
    edit() {
      console.log('修改')
      this.$emit('editDepart', this.nodeData)
    },
    async del() {
      console.log('删除')
      console.log(this.nodeData)
      try {
        await this.$confirm('你确定要删除该部门吗', '温馨提示', {
          type: 'warning'
        })
      } catch {
        return
      }
      // 删除部门
      await delDepartment(this.nodeData.id)
      // 提示删除成功
      this.$message.success('删除成功')
      // 重新渲染数据,通知父组件更新数据
      this.$emit('getDepartments')
    }
  }

}
</script>

<style>
.el-col {
  height: 30px;
  line-height: 30px;
}
.el-icon-caret-right:before {
  content: "\e723";
  color: #000;
}
.el-tree-node__expand-icon.expanded {
  transform: rotate(0deg);
}
.el-tree-node__expand-icon.expanded::before {
  content: "\e722";
  color: #000;
}

.el-tree-node__expand-icon.is-leaf::before {
  content: "\e7a5";
  color: #000;
}
</style>
